package edu.zzuli.assistant.action;

import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import edu.zzuli.assistant.bean.FindPsw;
import edu.zzuli.assistant.bean.User;
import edu.zzuli.assistant.service.FindPswService;
import edu.zzuli.assistant.service.UserService;
import edu.zzuli.assistant.service.impl.FindPswServiceImpl;
import edu.zzuli.assistant.service.impl.UserServiceImpl;
import edu.zzuli.assistant.until.StrUtil;

public class FindPswAction  extends ActionSupport implements ModelDriven{

	private UserService userService = new UserServiceImpl();
	private FindPswService findPswService = new FindPswServiceImpl();
	private FindPsw findPsw;
	
	public FindPsw getFindPsw() {
		return findPsw;
	}
	public void setFindPsw(FindPsw findPsw) {
		this.findPsw = findPsw;
	}
	
	@Override
	public Object getModel() {
		// TODO Auto-generated method stub
		if(findPsw==null){
			this.findPsw = new FindPsw();
		}
		return findPsw;
	}
	/**
	 * @Description 跳转到忘记密码页面
	 * @return
	 */
	public String forget(){
		
		return "forget";
	}
	
	/**
	 * @Description 检查key
	 * @return
	 */
	public String check(){
		if(findPsw.getKey()!=null){
			findPsw = findPswService.getByKey(findPsw.getKey());
			if(findPsw!=null && new Date().getTime() < findPsw.getFailTime().getTime()){
					//没有过期
				return "reset";
			}
			return "miss";
		}
		return "miss";
	}
		/**
		 * @Description 更改密码
		 * @return
		 */
		public void reset(){
			if(findPsw.getKey()!=null){
				findPsw = findPswService.getByKey(findPsw.getKey());
				if(findPsw!=null && new Date().getTime() < findPsw.getFailTime().getTime()){
					//没有过期
					// 验新密码可以修改
					HttpServletRequest request = ServletActionContext.getRequest();
					String newPassword = request.getParameter("newPassword");
					if(newPassword==null || newPassword.length()<6 || newPassword.length()>12){
						StrUtil.sendMsg("data_error");
					}
					else{
						User user = new User();
						user.setId(findPsw.getUserId());
						userService.modifyPassword(user, newPassword);
						findPswService.delete(findPsw.getUserId());
						StrUtil.sendMsg("success");
					}
				}else{
					StrUtil.sendMsg("miss");
				}
			}
			else{
				StrUtil.sendMsg("miss");
			}
			
	}
	
	/**
	 * @Description 发送邮件
	 */
	public void send(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String path = request.getScheme()+"://"+ request.getServerName() + ":" + request.getServerPort()+ request.getContextPath()+"/"+"findpsw_check.action";
		if(findPsw.getEmail()==null || !isEmail(findPsw.getEmail())){
			StrUtil.sendMsg("data_error");
		}else{
			// 找到用户 根据邮箱
			User user = userService.selectByEmail(findPsw.getEmail());
			if(user==null){
				StrUtil.sendMsg("user_none");
			}else{
				findPsw.setUserId(user.getId());
				findPsw.setFailTime(StrUtil.getFailDate());
				findPsw.setKey(StrUtil.getKey());
				boolean result = findPswService.add(findPsw,path);
				if(result){
					StrUtil.sendMsg("success");
				}
				else{
					StrUtil.sendMsg("fail");
				}
			}
		}
		
	}
	
	// 邮箱正则表达式
    public boolean isEmail(String email){     
        String str="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
           Pattern p = Pattern.compile(str);     
           Matcher m = p.matcher(email);     
           return m.matches();    
      } 

}
